# 完成对音频的嵌入，提取特征 
import wav2clip
import librosa
import os
import numpy as np

def file_audio(root_path, root_path_label, type="wav"):
    # 1. 获取音频文件
    file_path = os.path.join(root_path, root_path_label)
    file_dir = [ i for i in os.listdir(file_path) if i.endswith("wav")]
    print(file_dir[:10], len(file_dir))
    file_path_real  = [os.path.join(file_path, x) for x in file_dir]
    file_save_name = [i.replace("wav", "npy") for i in file_dir]
    for index, i in enumerate(file_path_real):
        audio, sample_rate = librosa.load(i, sr=None)
        model = wav2clip.get_model()
        embeding_audio = wav2clip.embed_audio(audio, model)
        # print(embeding_audio, embeding_audio.shape)
        np.save(os.path.join(root_path, "embed_audio", root_path_label, file_save_name[index]), embeding_audio)


if __name__ == "__main__":
    file_audio("./data/trainset", "0")
    file_audio("./data/trainset", "1")

